package com.example.springredisson;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author bsg
 * @date 2021/11/6
 * @description  自定义缓存 aop
 */
@RestController
public class test {
    private static Logger logger = LoggerFactory.getLogger(test.class);



    /**
     * 本地缓存 测试   咖啡因
     * @return
     */
    @GetMapping("/test1")
    @Cacheable(value = "caffeine")
    public String test(User user) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "本地缓存测试";
    }


    /**
     * 远程缓存测试   redis
     * @return
     */
    @GetMapping("/test2")
    @Cacheable(value = "redis",cacheManager = "redisCacheManager")
    public String test2(User user) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "远程缓存测试缓存测试";
    }

    /**
     * 二级缓存 咖啡因  +redis  指定id
     * @param user
     * @return
     */
    @GetMapping("/test3")
    @CacheLevel(value = "cacheLevel",id ="#user.id")
    public User test3(User user) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        user.setRemark("二级缓存测试 指定id");
        return user;
    }


    /**
     * 删除二级缓存  指定id
     * @param user
     * @return
     */
    @GetMapping("/test4")
    @CacheLevel(value = "cacheLevel",id ="#user.id",type =CacheLevelEnum.DELETE)
    public String test4(User user) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "删除成功";
    }


    /**
     * 二级缓存 咖啡因  +redis  未指定id
     * @param user
     * @return
     */
    @GetMapping("/test5")
    @CacheLevel(value = "cacheLevel")
    public User test5(User user) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        user.setRemark("二级缓存测试 不指定id");
        return user;
    }

    /**
     * 删除二级缓存  未指定id
     * @param user
     * @return
     */
    @GetMapping("/test6")
    @CacheLevel(value = "cacheLevel",type = CacheLevelEnum.DELETE)
    public String test6(User user) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        user.setRemark("二级缓存测试 不指定id");
        return "删除成功";
    }




}
